/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cl.jsoftlabs.gwt.soccer.server.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

import cl.jsoftlabs.gwt.soccer.client.bean.FutbolClubVO;
import cl.jsoftlabs.gwt.soccer.client.services.FutbolClubService;
import cl.jsoftlabs.gwt.soccer.server.persistence.entities.FutbolClub;
import cl.jsoftlabs.gwt.soccer.server.persistence.entities.UtilEntities;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class FutbolClubServiceImpl extends RemoteServiceServlet implements FutbolClubService {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3337193637093488781L;

	@Override
	public List<FutbolClubVO> obtenerClubes() {
		List<FutbolClubVO> vos = new ArrayList<FutbolClubVO>();
		List<FutbolClub> items = findAll();
		for (Iterator<FutbolClub> iterator = items.iterator(); iterator.hasNext();) {
			FutbolClub fc = (FutbolClub) iterator.next();
			//System.out.println(fc.getPais().getNombrePais()+" - "+fc.getNombreClub());
			vos.add(UtilEntities.toVO(fc));
		}
		return vos;
	}
	
	@PersistenceContext
	EntityManager em;
	
	@Resource private UserTransaction utx;
	
	@SuppressWarnings("unchecked")
	public List<FutbolClub> findAll() {
		List<FutbolClub> items  = em.createQuery("Select fc from FutbolClub fc ORDER BY fc.idFutbolClub").getResultList();
		return items;
	}
	
	@Override
	public FutbolClubVO obtenerClub(Integer idFC) {
		Query query = em.createQuery("Select fc from FutbolClub fc Where fc.idFutbolClub = :idFutbolClub");
		query.setParameter("idFutbolClub", idFC);
		try {
			FutbolClub entity  = (FutbolClub) query.getSingleResult();
			return UtilEntities.toVO(entity);
		} catch (NoResultException e) {
			System.out.println("SIN RESULTADOS AL BUSCAR UN CLUB DE FUTBOL");
		}
		return null;
	}

	@Override
	public int guardarClub(FutbolClubVO fc) {
		FutbolClub fce = UtilEntities.toEntity(fc);
		try {
			utx.begin();
			if (fc.getIdFutbolClub() == null || fc.getIdFutbolClub() == 0) {
				System.out.println("Nuevo Registro "+fce);
				fce.setIdFutbolClub(generarIdPais());
				em.persist(fce);
			}else{
				System.out.println("Modifica Registro "+fce);
				em.merge(fce);
			}
			utx.commit(); 
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 2;
	}
	
	public Integer generarIdPais(){
		Long id = null;
		
		Query query = em.createNativeQuery("SELECT nextval('futbol_club_seq')");
		id = (Long) query.getSingleResult();

		return id.intValue();
	}
	
}
